
package loginRegister;

/**
 *
 * @author 黄忠
 */
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class LoginBean {
    private String userName;
    private String password;
    private Connection con=null;
    private Statement stmt=null;
    private ResultSet rs=null;
    public LoginBean(){
        try {
            /*
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("CREATE TABLE IF NOT EXISTS `dgutInfor` (\n" +
                    "      `username` char(12) NOT NULL UNIQUE,\n" +
                    "      `name` varchar(11) NOT NULL,\n" +
                    "      `group` varchar(11) NOT NULL,\n" +
                    "      `faculty_title` varchar(30) NOT NULL,\n" +
                    "       PRIMARY KEY (`username`)\n" +
                    "    );");
            stmt.executeUpdate("CREATE TABLE IF NOT EXISTS `localInfor` (\n" +
                    "      `username` char(30) NOT NULL UNIQUE,\n" +
                    "      `password` varchar(30) NOT NULL,\n" +
                    "      `xing` varchar(11) NOT NULL,\n" +
                    "      `ming` varchar(11) NOT NULL,\n" +
                    "      `tx` varchar(30) NOT NULL DEFAULT 'tx.jpg',\n" +
                    "      `dgut_username` char(12),\n" +
                    "       PRIMARY KEY (`username`),\n" +
                    "      FOREIGN KEY (`dgut_username`) REFERENCES `dgutInfor`(`username`)\n" +
                    "    );");
            stmt.close();
            conn.close();
            context.close();
            */
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public boolean login(){
        boolean result=false;
        try{
            connectSQL();
            String sql="select * from user where userName='"+userName+"' AND password='"+password+"'";
            rs=stmt.executeQuery(sql);
            System.out.println("result is :");
            if(rs.next()){
                result=true;
            }
            closeSQL();
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }
    public boolean connectSQL(){
        try {
        Class.forName("org.h2.Driver");
        String url="jdbc:h2:~/eazenSQL";
        con= DriverManager.getConnection(url,"eazen","201741412126");
        stmt=con.createStatement();
        return true;
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    public boolean closeSQL(){
        try {
            rs.close();
            stmt.close();
            con.close();
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    public boolean register(){
        boolean result=false;
        try{
            connectSQL();
            String sql="select * from user where userName='"+userName+"'";
            rs=stmt.executeQuery(sql);
            if(!rs.next()) {
                sql="insert into user"+"(userName,password)"+"values("+"'"+userName+"'"+","+"'"+password+"'"+")";
                stmt.executeUpdate(sql);
                result=true;
            }
            closeSQL();
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
